/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-29 18:14:18
 * @Description  : 用户文件管理页面
 */

import { defineStore } from 'pinia'
import type { SourceNav } from '@/types/source'
import type { IndexObj } from '@/types/commom'
import {
  CUR_SOURCE_NAV,
  HISTORY_SOURCE_NAVS,
  SOURCE_SHOW_DESC,
  SOURCE_VIEW_MODEL,
  CUR_SOURCE_DESC,
  UPLOAD_TASKS,
  NOTICE_LISTS,
  LINK_SHARE_CODE
} from '@/constants'
import { getItem, removeItem, setItem } from '@/utils/storage'
import type { UploadTask } from '@/types/file'
import type { NoticeRes } from '@/types/notice'

export default defineStore('explorer', {
  state(): {
    historyNavs: SourceNav[]
    curNav: SourceNav | undefined
    navIndex: number
    viewModel: 'list' | 'grid' // 视图模式
    showDesc: string
    cutSourceList: SourceNav[]
    cutModel: 'copy' | 'cut' // 剪切模式
    curSourceDesc: SourceNav | undefined
    uploadTasks: UploadTask[]
    notices: NoticeRes[]
    linkShareCode: string
  } {
    return {
      historyNavs: getItem(CUR_SOURCE_NAV) ? [getItem(CUR_SOURCE_NAV)] : [],
      curNav: getItem(CUR_SOURCE_NAV) || undefined,
      navIndex: 0,
      viewModel: getItem(SOURCE_VIEW_MODEL) || 'list',
      showDesc: getItem(SOURCE_SHOW_DESC) || '1',
      cutSourceList: [],
      cutModel: 'copy',
      curSourceDesc: getItem(CUR_SOURCE_DESC) || undefined,
      uploadTasks: getItem(UPLOAD_TASKS) || [],
      notices: getItem(NOTICE_LISTS) || [],
      linkShareCode: getItem(LINK_SHARE_CODE) || ''
    }
  },
  getters: {
    canforword: (state) => {
      return state.navIndex < state.historyNavs.length - 1
    },
    canBack(state) {
      return state.navIndex > 0
    }
  },
  actions: {
    setHistoryNav(historyNavs: SourceNav[]) {
      this.historyNavs = historyNavs
      setItem(HISTORY_SOURCE_NAVS, historyNavs)
    },

    pushHistoryNav(nav: SourceNav) {
      if (
        this.historyNavs.length &&
        this.historyNavs[this.historyNavs.length - 1].key === nav.key
      ) {
        return
      }
      this.historyNavs.push(nav)
      this.navIndex = this.historyNavs.length - 1
      this.setCurNav(this.historyNavs[this.navIndex])
      setItem(HISTORY_SOURCE_NAVS, this.historyNavs)
    },

    setCurNav(curNav: SourceNav | undefined) {
      this.curNav = curNav
      setItem(CUR_SOURCE_NAV, curNav)
    },

    setCurSourceDesc(curSourceDesc: SourceNav | undefined) {
      this.curSourceDesc = curSourceDesc
      setItem(CUR_SOURCE_DESC, curSourceDesc)
    },

    backNavIndex() {
      if (this.navIndex <= 0) {
        return
      }
      this.navIndex -= 1
      if (this.navIndex < this.historyNavs.length) {
        this.setCurNav(this.historyNavs[this.navIndex])
      }
    },

    nextNavIndex() {
      if (this.navIndex >= this.historyNavs.length - 1) {
        return
      }
      this.navIndex += 1
      this.setCurNav(this.historyNavs[this.navIndex])
    },

    setSourceViewModel(viewModel: 'list' | 'grid') {
      this.viewModel = viewModel
      setItem(SOURCE_VIEW_MODEL, viewModel)
    },

    setShowSourceDesc(showDesc: string) {
      this.showDesc = showDesc
      setItem(SOURCE_SHOW_DESC, showDesc)
    },

    setCopySourceList(sourceList: IndexObj[]) {
      this.cutModel = 'copy'
      this.cutSourceList = sourceList
    },

    setCutSourceList(sourceList: IndexObj[]) {
      this.cutModel = 'cut'
      this.cutSourceList = sourceList
    },

    setUploadTaskList(uploadTasks: UploadTask[]) {
      this.uploadTasks = uploadTasks
      setItem(UPLOAD_TASKS, uploadTasks)
    },

    setNotices(notices: NoticeRes[]) {
      this.notices = notices
      setItem(NOTICE_LISTS, notices)
    },

    cleanNotices() {
      this.notices = []
      setItem(NOTICE_LISTS, [])
    },

    setLinkShareCode(code: string) {
      this.linkShareCode = code
      setItem(LINK_SHARE_CODE, code)
    },

    initExplorerStore() {
      this.historyNavs = []
      this.curNav = undefined
      this.curSourceDesc = undefined
      this.navIndex = 0
      this.viewModel = 'list'
      this.showDesc = '1'
      this.cutSourceList = []
      this.cutModel = 'copy'
      removeItem(CUR_SOURCE_NAV)
      removeItem(SOURCE_VIEW_MODEL)
      removeItem(SOURCE_SHOW_DESC)
    }
  }
})
